﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using FSViet.Survey.Helpers;
using System.Data;
using System.Web.UI.HtmlControls;

namespace FSViet.Survey.Administration.Group
{
	public partial class List : ListPageBase
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			CheckCampaign();

			if (!IsPostBack)
			{
				LoadData();
			}
		}

		void LoadData()
		{
			//int page = PageIndex - 1;
			//if (page < 0) page = 0;
			try
			{
				DataHelper dataH = new DataHelper();
				int totalRecord = 0;
				var data = dataH.Load(
					"SELECT COUNT(*) FROM Groups WHERE CampaignId=@CampaignId",
					"SELECT * FROM Groups WHERE CampaignId=@CampaignId ORDER BY Created",
					0,
					100,
					out totalRecord,
					dataH.CreateParam("CampaignId", System.Data.SqlDbType.Char, GlobalApplication.GetGuidS(CampaignId)));

				//ShowPage(totalRecord, GlobalApplication.GetGuidS(CampaignId));

				ListItem.DataSource = data;
				ListItem.DataBind();

				try
				{
					ListItem.Columns[3].Visible = QuestionId != Guid.Empty;
					ListItem.Columns[4].Visible = ListItem.Columns[3].Visible;
				}
				catch (Exception ex)
				{

				}
			}
			catch (Exception ex)
			{
				msg.Text = ex.Message;
#if DEBUG
				msg.Text = ex.ToString();
#endif
			}
		}

		void ShowPage(int totalRecord, string siteId)
		{
			if (totalRecord > GlobalApplication.ItemPerPage)
			{ }
			else
			{
				PageList.Visible = false;
				return;
			}
			int totalPage = totalRecord / GlobalApplication.ItemPerPage + 1;
			var ul = new HtmlGenericControl("ul");
			for (int i = 1; i <= totalPage; i++)
			{
				var li = new HtmlGenericControl("li");
				li.Controls.Add(new HyperLink() { Text = i.ToString(), NavigateUrl = "?p=" + i.ToString() });
				ul.Controls.Add(li);
			}
			PageList.Controls.Add(ul);
		}

		string BuildPramToDetails()
		{
			string result = string.Empty;

			return result;
		}

		protected void ListItem_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "AssignGroup")
			{
				#region assign to group
				
				var dataH = new DataHelper();
				try
				{
					dataH.Execute("UPDATE Questions SET GroupId=@GroupId WHERE Id=@Id",
						dataH.CreateParam("GroupId",SqlDbType.Char,e.CommandArgument),
						dataH.CreateParam("Id", SqlDbType.Char, GlobalApplication.GetGuidS(QuestionId)));

					Response.Redirect("../Question/List.aspx");
				}
				catch (Exception ex)
				{
					msg.Text = ex.Message;
#if DEBUG
					msg.Text = ex.ToString();
#endif
				}

				#endregion
			}
			else if (e.CommandName == "AssignAnswerGroup")
			{
				#region assign to group

				var dataH = new DataHelper();
				try
				{
					dataH.Execute("UPDATE Groups SET AnswerGroupId=@AnswerGroupId WHERE Id=@Id",
						dataH.CreateParam("Id", SqlDbType.Char, e.CommandArgument),
						dataH.CreateParam("AnswerGroupId", SqlDbType.Char, GlobalApplication.GetGuidS(QuestionId)));

					Response.Redirect("../Question/List.aspx");
				}
				catch (Exception ex)
				{
					msg.Text = ex.Message;
#if DEBUG
					msg.Text = ex.ToString();
#endif
				}

				#endregion
			}
		}

		protected void ListItem_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				var drv = (DataRowView)e.Row.DataItem;
				var btn = e.Row.FindControl("AssignGroupButton") as Button;
				if (btn != null)
				{
					btn.CommandArgument = drv["Id"].ToString();
					btn.CommandName = "AssignGroup";
				}

				btn = e.Row.FindControl("AssignAnswerGroupButton") as Button;
				if (btn != null)
				{
					btn.CommandArgument = drv["Id"].ToString();
					btn.CommandName = "AssignAnswerGroup";
				}
			}
		}
	}
}